package com.yy.ant.sys.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.ant.sys.entity.Role;
import com.yy.ant.sys.service.IRoleService;
import com.yy.model.ResultMsg;
import com.yy.model.ResultTable;
import com.yy.model.enums.ResultEnum;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyingying
 * @since 2019-12-20
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {

	
	@Autowired
	private IRoleService iRoleService;
	
	/**
	 * select 数据获取
	 * @return
	 */
	@RequestMapping(value = "/listSelect")
	public ResultMsg listSelect() {
		List<Role> list=iRoleService.list();
		List<HashMap<String, Object>> lHashMaps=new ArrayList<HashMap<String,Object>>();
		for (Role o:list) {
			HashMap<String, Object> hashMap=new HashMap<String, Object>();
			hashMap.put("name", o.getName());
			hashMap.put("value", o.getId());
			lHashMaps.add(hashMap);
		}
		ResultMsg resultMsg=new ResultMsg();
		if (list.size()>0) {
			resultMsg.setData(lHashMaps);
			resultMsg.setSuccess(true);
			resultMsg.setMsg(ResultEnum.sucess_create_failed.getMsg());
		}else {
			resultMsg.setMsg(ResultEnum.error_create_failed.getMsg());
		}
		
		return resultMsg;
	}
	@RequestMapping(value = "/getbyId")
	public ResultMsg ById(@RequestParam(value = "id", required = false) String id) {
		ResultMsg resultMsg = new ResultMsg();
		Role user =iRoleService.getById(id);
		resultMsg.setData(user);
		return resultMsg;
	}
	
	/**
	 * 分页查询
	 * @param str
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping(value = "/listPage")
	public ResultTable<Role> listPage(@RequestParam(value = "str",required = false) String str,
			@RequestParam(value = "page") String page,@RequestParam(value = "limit") String limit){
		JSONObject jsonObject=JSONObject.parseObject(str);
		QueryWrapper<Role> queryWrapper=new QueryWrapper<Role>();
		if (jsonObject!=null) {
			if (StringUtils.isNotEmpty(jsonObject.getString("startTime"))&& StringUtils.isNotEmpty(jsonObject.getString("endTime"))) {
				queryWrapper.between("createtime", jsonObject.getString("startTime"), jsonObject.getString("endTime"));
			}
			if (StringUtils.isNotEmpty(jsonObject.getString("name"))) {
				queryWrapper.like("name", jsonObject.getString("name"));
			}
		}
		
		IPage<Role> iPage=iRoleService.page(new Page<>(Integer.valueOf(page),Integer.valueOf(limit)),queryWrapper);
		ResultTable<Role> uResultTable=new ResultTable<Role>();
		uResultTable.setCount(iPage.getTotal());
		uResultTable.setData(iPage.getRecords());
		return uResultTable;
	}
	
	
	
	/**
	 * 新增
	 * @param str
	 * @return
	 */
	@RequestMapping(value = "/add")
	public ResultMsg add(@RequestParam(value = "str",required = false)String str) {
		Role role=JSON.parseObject(str, Role.class);
		role.setCreatetime(new Date());
		ResultMsg resultMsg=new ResultMsg();
		boolean b=iRoleService.save(role);
		if (b) {
			resultMsg.setSuccess(true);
			resultMsg.setMsg(ResultEnum.sucess_create_failed.getMsg());
		}else {
			resultMsg.setMsg(ResultEnum.error_create_failed.getMsg());
		}
		
		return resultMsg;
	}
	/**
	 * 删除
	 * @param str
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public ResultMsg delete(@RequestParam(value = "str",required = false)String str) {
		List<Long> ids = new ArrayList<>();
        String[] id = str.split(",");
        for (int i = 0; i < id.length; i++) {
        	if (StringUtils.isNotEmpty(id[i])) {
        		ids.add(Long.valueOf(id[i]));
			}
        }
        ResultMsg resultMsg=new ResultMsg();
        boolean b=iRoleService.removeByIds(ids);
		if (b) {
			resultMsg.setSuccess(true);
			resultMsg.setMsg(ResultEnum.sucess_delete_failed.getMsg());
		}else {
			resultMsg.setMsg(ResultEnum.error_delete_failed.getMsg());
		}
		return resultMsg;
	}
	
	
	
	/**
	 * 修改
	 * @param str
	 * @return
	 */
	@RequestMapping(value = "/update")
	public ResultMsg update(@RequestParam(value = "str",required = false)String str) {
		Role role=JSON.parseObject(str, Role.class);
		ResultMsg resultMsg=new ResultMsg();
        boolean b=iRoleService.updateById(role);
        if (b) {
        	resultMsg.setSuccess(true);
			resultMsg.setMsg(ResultEnum.sucess_update_failed.getMsg());
		}else {
			resultMsg.setMsg(ResultEnum.error_update_failed.getMsg());
		}
		return resultMsg;
	}
	
	
	
}
